import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // TRY THIS: Try running your application with "flutter run". You'll see
        // the application has a purple toolbar. Then, without quitting the app,
        // try changing the seedColor in the colorScheme below to Colors.green
        // and then invoke "hot reload" (save your changes or press the "hot
        // reload" button in a Flutter-supported IDE, or press "r" if you used
        // the command line to start the app).
        //
        // Notice that the counter didn't reset back to zero; the application
        // state is not lost during the reload. To reset the state, use hot
        // restart instead.
        //
        // This works for code too, not just values: Most code changes can be
        // tested with just a hot reload.
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: '剪裁和容器'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    Widget avatar = Image.asset("imgs/avatar.png", width: 60.0);   //导入图片
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            avatar, //不剪裁
            ClipOval(child: avatar), //子组件为正方形时剪裁成内贴圆形；为矩形时，剪裁成内贴椭圆
            ClipRRect( //将子组件剪裁为圆角矩形
              borderRadius: BorderRadius.circular(5.0),
              child: avatar,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Align(
                  alignment: Alignment.topLeft,
                  widthFactor: .5,//宽度设为原来宽度一半，另一半会溢出，但溢出部分仍会显示
                  child: avatar,
                ),
                const Text("你好世界", style: TextStyle(color: Colors.green),)//写入文本
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                ClipRect(//将溢出部分剪裁
                  child: Align(
                    alignment: Alignment.topLeft,
                    widthFactor: .5,//宽度设为原来宽度一半
                    child: avatar,
                  ),
                ),
                const Text("你好世界",style: TextStyle(color: Colors.green))
              ],
            ),
            const SizedBox(height: 10),
            // 自定义裁剪
            DecoratedBox(
              decoration: const BoxDecoration(
                color: Colors.red
              ),
              child: ClipRect(
                clipper: MyClipper(), //使用自定义的clipper
                child: avatar
              ),
            ),
            // 容器组件演示
            Container(
              margin: const EdgeInsets.only(top: 50.0, left: 120.0),
              constraints: const BoxConstraints.tightFor(width: 200.0, height: 150.0),
              decoration: const BoxDecoration(
                gradient: RadialGradient(
                  colors: [Colors.red, Colors.orange],
                  center: Alignment.topLeft,
                  radius: .98,
                ),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black54,
                    offset: Offset(2.0, 2.0),
                    blurRadius: 4.0,
                  )
                ],
              ),
              transform: Matrix4.rotationZ(.2),
              alignment: Alignment.center,
              child: const Text(
                "5.20", style: TextStyle(color: Colors.white, fontSize: 40.0),
              ),
            ),
            Container(
              margin: const EdgeInsets.all(20.0),
              color: Colors.orange,
              child: const Text("Hello world!"),
            ),
            Container(
              padding: const EdgeInsets.all(20.0),
              color: Colors.orange,
              child: const Text("Hello world!"),
            ),
            Padding(
              padding: const EdgeInsets.all(20.0),
              child: DecoratedBox(
                decoration: const BoxDecoration(color: Colors.orange),
                child: const Text("Hello world!"),
              ),
            ),
            DecoratedBox(
              decoration: const BoxDecoration(color: Colors.orange),
              child: Padding(
                padding: const EdgeInsets.all(20.0),
                child: const Text("Hello world!"),
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

// 自定义Clipper
class MyClipper extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) => Rect.fromLTWH(10.0, 15.0, 40.0, 30.0);//截取图片中部40×30像素的范围

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) => false;
}
